import webapp2
import re
from google.appengine.ext import db

class Image(db.Model):
  data = db.BlobProperty()

class ImagesRequestHandler(webapp2.RequestHandler):
  def get(self):
    id = self.request.path.replace('/imagesDB/','')
    if not re.match(r'^[a-zA-Z0-9/\-+]+$', id):
      self.response.out.write('invalid image key')

    image = self.__findImageByKey(id)
    if image:
      self.response.out.write(image.data)
    else:
      self.response.out.write('no image found for id')
  
  def __findImageByKey(self, id):
    try:
      return db.get(id)
    except:
      return None
